package com.android.lovertest;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class ChooseHostActivity extends Activity {
	public static final int gvHostPics[] = { R.drawable.host1,
			R.drawable.host2, R.drawable.host3, R.drawable.host4 };
	public static final String HOSTINDEX = "hostindex";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);

		LinearLayout ll = new LinearLayout(this);
		ll.setOrientation(LinearLayout.VERTICAL);

		TextView tv = new TextView(this);
		tv.setGravity(Gravity.CENTER);
		tv.setText("Choose Host");
		ll.addView(tv);

		GridView gv = new GridView(this);
		gv.setNumColumns(2);
		gv.setAdapter(new HostGridViewAdapter(this));

		gv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Intent intent = new Intent(ChooseHostActivity.this,
						LoverTest.class);
				intent.putExtra(HOSTINDEX, arg2);
				startActivity(intent);
				finish();
			}
		});

		ll.addView(gv);

		setContentView(ll);
	}
}

class HostGridViewAdapter extends BaseAdapter {

	private Context context;

	public HostGridViewAdapter(Context context) {
		this.context = context;
	}

	@Override
	public int getCount() {
		return ChooseHostActivity.gvHostPics.length;
	}

	@Override
	public Object getItem(int arg0) {
		return ChooseHostActivity.gvHostPics[arg0];
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(int arg0, View arg1, ViewGroup arg2) {

		ImageView iv = null;

		if (arg1 == null) {
			iv = new ImageView(context);
			iv.setLayoutParams(new GridView.LayoutParams(150, 150));
			iv.setPadding(10, 10, 10, 10);
		} else {
			iv = (ImageView) arg1;
		}

		iv.setImageResource(ChooseHostActivity.gvHostPics[arg0]);
		return iv;
	}
}
